package com.wh.wisdomsite.xbox.server.controller.du;

import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.du.entity.DustDevRecord;
import com.wh.wisdomsite.xbox.du.service.IDustDevRecordService;
import com.wh.wisdomsite.xbox.pojo.du.DustDevRecordIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.server.util.WrapperUtil;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 扬尘设备运行记录controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.du
 * @date 2018年08月06日 22:47
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/dustDevRecord")
public class DustDevRecordController {
    private static final Log logger = LogFactory.getLog(DustDevRecordController.class);

    @Autowired
    private IDustDevRecordService iDustDevRecordService;

    /**
     * 跳转到扬尘设备运行数据
     *
     * @return
     */
    @RequiresPermissions("rules:du_dustDevRecord_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index(HttpServletRequest request ) {
        String prId = request.getParameter("prId");
        request.setAttribute("prId",prId);
        return "du/dustDevRecord/dustDevHistoricalDataList";
    }

    /**
     * 获取扬尘设备运行数据
     *
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:du_dustDevRecord_find")
    @RequestMapping(value = "/dustDevHistoricalData.do")
    public void dustDevRecordList(HttpServletResponse response, DustDevRecordIn in) {
        try {
            logger.info("加载扬尘设备运行数据列表-->>开始");
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<Project> page = new Page<Project>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            WrapperUtil<DustDevRecord> ewDustDevRecord = new WrapperUtil<DustDevRecord>();
            ewDustDevRecord.setEntity(new DustDevRecord());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewDustDevRecord.eq("pr.prId",in.getPrId());
            }else{
                //如果没有项目id，则不让数据查询出结果
                ewDustDevRecord.eq("pr.prId",-1);
            }
            if(StringUtils.isNotEmpty(in.getDeviceId())){
                ewDustDevRecord.eq("dp.deviceId",in.getDeviceId());
            }
            if(StringUtils.isNotEmpty(in.getStartDate()) && StringUtils.isNotEmpty(in.getEndDate())){
                ewDustDevRecord.between("d.collectTime",in.getStartDate(),in.getEndDate()+" "+"23:59:59");
            }
            Page<Map<String, Object>> dustDevRecordPage = iDustDevRecordService.getMapsPage(page, ewDustDevRecord);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if (dustDevRecordPage.getRecords().size() > 0) {
                for(Map<String, Object> record : dustDevRecordPage.getRecords()){
                    JSONObject object = new JSONObject(record);
                    if(record.get("dustProCheckPoint") != null){
                        object.put("dustProCheckPoint",new JSONObject(record.get("dustProCheckPoint")));
                    }
                    if(record.get("project") != null){
                        object.put("project",new JSONObject(record.get("project")));
                    }
                    array.put(object);
                }
            }
            json.put("total", dustDevRecordPage.getTotal());
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
            logger.info("加载扬尘设备运行数据列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 扬尘设备运行数据详情
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("dustDevRecordDesc.do")
    public String dustDevRecordDesc(String id, HttpServletRequest request) {
        logger.info("扬尘设备运行数据详情-->>开始");
        Map<String , Object> dustDevRecord = iDustDevRecordService.getMapById(id);
        request.setAttribute("dustDevRecord", dustDevRecord);
        logger.info("扬尘设备运行数据详情-->>结束");
        return "du/dustDevRecord/dustDevRecordDesc";
    }

}
